import Vue from 'vue'
import Vuex from 'vuex'
Vue.use(Vuex)

const store = new Vuex.Store({
	state: {
		diningTableList: [], //桌位列表
		orderList: [], //订单列表
		categoryList: [], //分类列表
		dishList: [], //菜品列表
	},
	mutations: {
		//保存桌位数据
		setDiningTableData(state, data) {
			state.diningTableList.push(data);
			uni.setStorageSync("diningTableList", JSON.stringify(state.diningTableList))
		},
		//删除桌位
		deleteDiningTableData(state, data) {
			state.diningTableList = state.diningTableList.filter(item => item.id !== data.id);
			uni.setStorageSync("diningTableList", JSON.stringify(state.diningTableList))
		},
		//获取桌位数据
		getDiningTableData(state, data) {
			state.diningTableList = data;
		},
		//更新餐桌状态
		updateDiningTableStatus(state, data) {
			state.diningTableList = state.diningTableList.map(item => {
				return item.id === data.id ? {
					...item,
					...data
				} : item;
			})

			uni.setStorageSync("diningTableList", JSON.stringify(state.diningTableList));
		},


		//保存菜品分类
		saveCategory(state, data) {
			state.categoryList.push(data);
			uni.setStorageSync("categoryList", JSON.stringify(state.categoryList));
		},
		//更新菜品分类
		updateCategory(state, data) {
			state.categoryList = state.categoryList.map(item => {
				return item.id === data.id ? {
					...item,
					...data
				} : item;
			})
			uni.setStorageSync("categoryList", JSON.stringify(state.categoryList));
		},
		//获取菜品分类
		getCategory(state, data) {
			state.categoryList = data;
		},
		//删除菜品分类
		deleteCategory(state, data) {
			state.categoryList = state.categoryList.filter(item => item.id !== data.id);
			uni.setStorageSync("categoryList", JSON.stringify(state.categoryList))
		},

		//保存菜品
		saveDish(state, data) {
			state.dishList.unshift(data);
			uni.setStorageSync("dishList", JSON.stringify(state.dishList));
		},
		//更新菜品
		updateDish(state, data) {

			for (var index = 0; index < state.dishList.length; index++) {
				var element = state.dishList[index];
				if (element.id === data.id) {
					state.dishList[index] = data;
				}

			}
			state.dishList = JSON.parse(JSON.stringify(state.dishList))

			uni.setStorageSync("dishList", JSON.stringify(state.dishList));
		},

		//获取菜品
		getDish(state, data) {
			state.dishList = data;
		},
		//删除菜品
		deleteDish(state, data) {
			state.dishList = state.dishList.filter(item => item.id !== data.id);
			uni.setStorageSync("dishList", JSON.stringify(state.dishList))
		},



		//保存记账数据
		setOrderData(state, data) {
			state.orderList.unshift(data);
			uni.setStorageSync("orderList", JSON.stringify(state.orderList))
		},
		//获取订单
		getOrderData(state, data) {
			state.orderList = data;
		},

	},
	actions: {

	},
	getters: {

	}
})

export default store